package com.example.maillist;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import item.Mail;
import service.mailService;

//import adapter.mainAdapter;

public class MainActivity extends AppCompatActivity {

    private ListView listview;
    private List<Mail> list;
    private mailService mailservice;
    private Mail m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        mailservice = new mailService(this);
        list = mailservice.queryAll();
        mainAdapter mainadapter = new mainAdapter();
        listview.setAdapter(mainadapter);

        EditText search =findViewById(R.id.searchtext);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText search =findViewById(R.id.searchtext);
                search.setText("");
            }
        });

        Button btn = findViewById(R.id.create);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                go();
            }
        });
        Button btn3 = findViewById(R.id.search);
        Button btn2 = findViewById(R.id.flush);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flush();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView searchText = findViewById(R.id.searchtext);
                if(searchText.getText() == null || searchText.getText() == "" || searchText.getText() == "请输入关键词来查询"){
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("创建成功");
                    builder.setPositiveButton("确定",null);
                    builder.show();
                    return;
                }
                list = mailservice.search(searchText.getText().toString());
                mainAdapter mainadapter = new mainAdapter();
                listview.setAdapter(mainadapter);
            }
        });

    }

    public void go() {
        Intent intent = new Intent(this, CreateActivity.class);
        startActivity(intent);
    }

    public void flush(){
        list = mailservice.queryAll();
        mainAdapter mainadapter = new mainAdapter();
        listview.setAdapter(mainadapter);
    }

    public void goUp(int id){
        Intent intent = new Intent(this, UpdateActivity.class);
        intent.putExtra("updateId",id);
        startActivity(intent);
    }

    public void init(){
        listview = findViewById(R.id.list);
//        listview.setOnItemClickListener(new MyOnItemClickListener());
    }

    private class mainAdapter extends BaseAdapter{
        public int getCount(){
            return list.size();
        }
        public Object getItem(int position){
            return list.get(position);
        }
        public long getItemId(int position){
            return position;
        }

        @SuppressLint("SetTextI18n")
            public View getView(int position, View convertView, ViewGroup parent){
            View item = convertView!=null?convertView:View.inflate(getApplicationContext(),R.layout.items,null);

            TextView nameItem = item.findViewById(R.id.name);
            TextView numberItem = item.findViewById(R.id.num);
            m = list.get(position);
            System.out.println("list:"+m.getId()+m.getName());

            nameItem.setText(m.getName()+"");
            numberItem.setText(m.getNumber()+"");

            Button btnDel = item.findViewById(R.id.btnDelete);
            Button btnUp = item.findViewById(R.id.btnUpdate);
            btnUp.setTag(m.getId());
            btnUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int tag = (Integer) v.getTag();

                    goUp(tag);
                }
            });
            btnDel.setTag(m.getId());
            btnDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                 android.content.DialogInterface.OnClickListener listener =
                         new android.content.DialogInterface.OnClickListener(){
                             @Override
                     public void onClick(DialogInterface dialog, int which) {
                                 int tag = (Integer) v.getTag();
                                 System.out.println("deLtag:"+tag);
                         list.remove(m);
                         mailservice.delete(tag);
                         flush();
                     }
                 };
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("确定要删除吗？");
                    builder.setPositiveButton("确定",listener);
                    builder.setNegativeButton("取消",null);
                    builder.show();
                }

            });

            return item;
        }
    }
}